<?php
namespace app\common\logic\home;
use app\common\model\LessonModel;
use app\common\model\LessonNameModel;
use app\common\model\UserModel;
use app\common\model\AppUserModel;
use app\common\model\RemindModel;
use app\common\logic\home\Template;
use app\common\model\ImgStatusModel;
use app\common\model\ImgModel;
use app\common\model\MaterialModel;
use app\common\logic\home\ImageApi;
use app\common\logic\Msg;

//课程
class Lesson
{
	public $tid = 14;
	//查询是否有课程
	public function index($user_id){
		$data_remind = RemindModel::get(['user_id'=>$user_id]);

		if(!empty($data_remind)){
			return [
					'code'=>-1,
					'msg'=>'已经提醒过了'
				];
		}
		$data = AppUserModel::get($user_id);
		if($data['role'] == 1){
			$grad_array = ['一年级'=>1,'二年级'=>2,'三年级'=>3,'四年级'=>4,'五年级'=>5,'六年级'=>6,'七年级'=>7,'八年级'=>8,'九年级'=>9];
			$grad = $grad_array[$data['grad']];
			$classes_array = [
								'一班'=>1,'二班'=>2,'三班'=>3,'四班'=>4,'五班'=>5,'六班'=>6,'七班'=>7,'八班'=>8,'九班'=>9,'十班'=>10,
								'十一班'=>11,'十二班'=>12,'十三班'=>13,'十四班'=>14,'十五班'=>15,'十六班'=>16,'十七班'=>17,'十八班'=>18,
								'十九班'=>19,'二十班'=>20,'二十一班'=>21,'二十二班'=>22,'二十三班'=>23,'二十四班'=>24,'二十五班'=>25,
								'二十六班'=>26,'二十七班'=>27,'二十八班'=>28,'二十九班'=>29,'三十班'=>30,'三十一班'=>31,'三十二班'=>32,
								'三十三班'=>33,'三十四班'=>34,'三十五班'=>35,'三十六班'=>36,'三十七班'=>37,'三十八班'=>38,'三十九班'=>39,
								'四十班'=>40,'四十一班'=>41,'四十二班'=>42,'四十三班'=>43,'四十四班'=>44,'四十五班'=>45,'四十六班'=>46,
								'四十七班'=>47,'四十八班'=>48,'四十九班'=>49,'五十班'=>50
							];
			$classes = $classes_array[$data['classes']];
			$schoolId = $data['schoolId'];
			$begin_time = date('Y-m-d');
			$end_time = date("Y-m-d",strtotime("-15 day"));
			$data_lesson = LessonModel::relation('names')
			                    ->where('school_id',$schoolId)
			                    ->where('grade',$grad)
			                    ->where('day','>=',$end_time)
			                    ->where('day','<=',$begin_time)
			                    ->order('day asc,end_time asc')
			                    ->select();
			                    
			if(empty($data_lesson->toArray())){
				return [
					'code'=>-1,
					'msg'=>'没有数据'
				];
			}
			$lesson_id_array = [];
			$status = 1;//不采集

			foreach ($data_lesson as $key => $value) {
				$lesson_id_array[$key] = $value['lesson_id'];
				if($value['names']['status'] == 2){
					$status = 2;
				}
				$end = $value['end_time'];
				$day = $value['day'];
			}
			$day_end = $day.' '.$end;
			//记录信息
			$data_rem = [
					'lesson_ids' => $lesson_id_array,
					'day' => $day,
					'end_time' => $day_end,
				];
				
			//不需要采集影像时
			if($status == 1){
				if(time() > strtotime($day_end)){
					
					
					$this->addRemind($user_id,$data_rem,5);//不需要识别 已发提醒短信 已完成  
					//将活动模板给于用户
					$Template = new Template();
					$taskid = $Template->index($this->tid,$user_id);     
					//发送完成模板短信提醒   
					$AliMsg = new Msg();
					$AliMsg->index1($data['phone'],$taskid);
					
			
				}else{
					$this->addRemind($user_id,$data_rem,1);//不需要识别 未发提醒短信 待完成
				}
			}else{
				//当需要采集图像时
				$this->collect($user_id,$data_rem);
			}
			 
		}else{
			return [
					'code'=>-1,
					'msg'=>'不是学生身份'
				];
		}
	}
	//当需要收集映像时
	public function collect($user_id,$data_rem){
		$data = AppUserModel::get($user_id);
		//是否已获取影像素材
		$rs = $this->upload($data_rem['day']);

		if($rs){
			
			//已有人脸图像
			if(!empty($data['avater'])){
				//人脸识别 
				$data_remind = RemindModel::get(['user_id'=>$user_id]);
			
				$temp = new Template();
				//将活动模板给于用户	
				$taskid = $temp->index($this->tid,$user_id);
				

				$rs = $this->discern($data['avater'],$data_rem['end_time'],$user_id);
				if($rs){
					//当匹配到将图库添加到素材库
					$this->addRemind($user_id,$data_rem,3);//记录提醒信息  需人脸识别 已识别成功
				}else{
					//当未匹配到
					$this->addRemind($user_id,$data_rem,2);//需人脸识别 但未识别成功 已完成  
				}
				

				//发送查看素材短信提醒   
				$AliMsg = new Msg();
				$rs = $AliMsg->index3($data['phone'],$taskid);
				dump($rs);
			   
			}else{
				//未上传自拍照
				//发送上传自拍短信提醒   
				$AliMsg = new Msg();
				$AliMsg->index2($data['phone']);

				$this->addRemind($user_id,$data_rem,-1);//需要人脸识别,已获取映像,但未上传自拍照 待完成 
			}
			
		}else{
			$this->addRemind($user_id,$data_rem,4);//需要人脸识别 但还未获取映像 待完成
		}
	}

	//需要人脸识别,已获取映像,但未上传自拍照
	public function rem_2($user_id){
		$data = RemindModel::where('user_id',$user_id)->where('status',-1)->find();
		
		if(!empty($data)){
			RemindModel::where('remind_id',$data['remind_id'])->update(['status'=>4]);
			//用户信息
			$data_app_user = AppUserModel::where('id',$user_id)->field('avater,phone')->find();
		
			$temp = new Template();
			//将活动模板给于用户	
			$taskid = $temp->index($this->tid,$user_id);
			
			$rs = $this->discern($data_app_user['avater'],$data['end_time'],$user_id);
			if($rs){
				//当匹配到将图库添加到素材库
				RemindModel::where('remind_id',$data['remind_id'])->update(['status'=>3]);//记录提醒信息  需人脸识别 已识别成功
			}else{
				//当未匹配到
				RemindModel::where('remind_id',$data['remind_id'])->update(['status'=>2]);//需人脸识别 但未识别成功 已完成  
			}
			
			//发送查看素材短信提醒   
			$AliMsg = new Msg();
			$AliMsg->index3($data_app_user['phone'],$taskid);
		}
	}
	
	//不需要采集影像 但未发提示短信的 定时任务
	public function rem_1(){
		$data = RemindModel::where('status',1)->select();
		$temp = new Template();
		$AliMsg = new Msg();
		foreach ($data as $key => $value) {
			$phone = AppUserModel::where('id',$value['user_id'])->value('phone');
			//修改提醒状态状态
			RemindModel::where('remind_id',$value['remind_id'])->update(['status'=>5]);//不需要识别 已发提醒短信 已完成  
			//将活动模板给于用户
			$taskid = $temp->index($this->tid,$value['user_id']); 
			//发送完成模板短信提醒   
			$AliMsg->index1($phone,$taskid);		
		}
	}

	//需要采集影像 但影像还未获取的 定时任务
	public function rem_4(){
      	
		if(date('Y-m-d H:i:s') >= date('Y-m-d 20:00:00')){
			$data = RemindModel::where('status',4)->limit(0,3)->select();
          	
			$temp = new Template();
			$AliMsg = new Msg();

			foreach ($data as $key => $value) {
				$data_app_user = AppUserModel::where('id',$value['user_id'])->field('avater,phone')->find();
				
				if(empty($data_app_user['avater'])){
					$AliMsg->index2($data_app_user['phone']);  
					RemindModel::where('remind_id',$value['remind_id'])->update(['status'=>-1]);//需要人脸识别,已获取映像,但未上传自拍照 待完成 
				}else{
               		//将活动模板给于用户	
					$taskid = $temp->index($this->tid,$value['user_id']);
					
					
					$rs = $this->discern($data_app_user['avater'],$value['end_time'],$value['user_id']);
					if($rs){
						//当匹配到将图库添加到素材库
						RemindModel::where('remind_id',$value['remind_id'])->update(['status'=>3]);//记录提醒信息  需人脸识别 已识别成功
					}else{
						//当未匹配到
						RemindModel::where('remind_id',$value['remind_id'])->update(['status'=>2]);//需人脸识别 但未识别成功 已完成  
					}
					//halt($data_app_user['phone']);
					
					//发送查看素材短信提醒   
					$AliMsg->index3($data_app_user['phone'],$taskid);
					    
				}

			}
		}else{
			echo 2;
		}
		
	}
	//添加提醒
	//1不需要人脸识别 未发提醒短信 5 不需要识别 已发提醒短信  4 需要人脸识别 但还未获取映像 -1 需要人脸识别,已获取映像,但未上传自拍照  2 需人脸识别 但未识别成功 3需人脸识别 已识别成功
	public function addRemind($user_id,$data_rem,$status=1){
		$data = [
				'user_id'=>$user_id,
				'lesson_ids' => implode(',', $data_rem['lesson_ids']),
				'end_time'=>$data_rem['end_time'],
				'day'=>$data_rem['day'],
				'status'=>$status,
			];
		$RemindModel = new RemindModel();
		$RemindModel->save($data);
		
	}


	
	//图像素材是否上传
	public function upload($day)
	{
		$status = ImgStatusModel::where('day',$day)->value('status');
		if($status > 2){
			return 1;
		}else{
			return 0;
		}
		
	}

	//图像匹配
	public function discern($avater,$end_time,$user_id){

		$ImageApi = new ImageApi();
		$time = strtotime($end_time);
		$begin_time = date('Y-m-d H:i:s',$time-60*60*24*5);
		$end_time =  date('Y-m-d H:i:s',$time+60*60*24*5);
		return $ImageApi->discern($avater,$begin_time,$end_time,$user_id);
	}
	
}
